/**************************************************************************************** 
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.Objects;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

/** 
 * <Description> <br> 
 *  
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr <br>
 */
@Service
public class IPRecordInfoHandler extends AbstractAttrHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;
    /**
     * Description: <br> 
     *  
     * @author tengg<br>
     * @taskId <br>
     * @param commonAttr <br>
     */ 
    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContextNew().getCurrentSceneInstId();
        KiteAttr kiteAttr = querySceneInstService.getCommonAttr(sceneInstId, commonAttr.getFieldName());
        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        if (Objects.nonNull(kiteAttr)) {
            // 单位名称
            commonAttr.setValue(kiteAttr.getValue());
            commonAttr.setValueDesc(kiteAttr.getValueDesc());
            if (KiteStringUtils.isEmpty(kiteAttr.getValue())) {
                commonAttr.setIsEdit(KeyConsts.IFTRUE_T);  //初始化可编辑有效
                kiteAttr.setIsEdit(KeyConsts.IFTRUE_T);   //写入实例中
                operateSceneInstService.setCommonAttr(sceneInstId, kiteAttr);
            }
            setCommonAttr(commonAttr);
        }
        
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
        if (KiteStringUtils.isNotEmpty(value)) {
            if ("cont_mail".equals(fieldName)) {
                KiteAttr mailAttr = querySceneInstService.getCommonAttr(sceneInstId, "cont_mail");
                if (Objects.nonNull(mailAttr)) {
                    if (KiteStringUtils.isEmpty(mailAttr.getValue())) {
                        mailAttr.setIsEdit("T");
                    }
                    operateSceneCommonService.updateCommonAttr(sceneInstId, sceneInst.getCustId(), mailAttr.getFieldName(), value, valueDesc);
                }
            }
            if ("card_no".equals(fieldName)) {
                KiteAttr cardNOAttr = querySceneInstService.getCommonAttr(sceneInstId, "card_no");
                if (Objects.nonNull(cardNOAttr)) {
                    cardNOAttr.setValue(value);
                    cardNOAttr.setValueDesc(valueDesc);
                    operateSceneInstService.setCommonAttr(sceneInstId, cardNOAttr);
                }
            }
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        return ErrorConsts.SUCCESS.getResult(true);
    }
}
